package com.simba.server.jsonservice.serivces;

import com.simba.common.jsonservice.JsonService;
import com.simba.common.log.LogUtil;
import com.simba.server.components.SlotManager;
import com.simba.server.components.SortControllerManager;
import com.simba.server.components.SortProtocolCreator;
import com.simba.server.components.data.SlotInfo;
import org.apache.log4j.Logger;
import org.jboss.netty.channel.Channel;
import org.json.JSONException;
import org.json.JSONObject;

import java.sql.Timestamp;

/**
 * @author create by kavin
 * @date 2020/10/18 20:06
 */
public class SlotPackJsonService implements JsonService {
    public static final String NAME = "SlotPack";
    private final static Logger logger = Logger.getLogger(SlotPackJsonService.class);

    @Override
    public void handleRequest(Channel channel, Object content) {
        try
        {
            JSONObject request = (JSONObject) content;
            String physicalSlotId = request.getString("slotId");

            SlotInfo slotInfo = SlotManager.getInstance().getSlotInfo(physicalSlotId);
            if (slotInfo!=null){
                slotInfo.setSlotType("1");
                SortControllerManager.getInstance().onSlotPackMessage(slotInfo);
            }

            Timestamp clearTime = SortProtocolCreator.getInstance().createCurrentTimeStamp();
            LogUtil.LogDebug(SlotPackJsonService.logger,
                    "Slot Pack Json Service request name : " + clearTime.toString());
        }
        catch (JSONException ex)
        {
            LogUtil.LogError(SlotPackJsonService.logger,
                    "Slot Pack Json Service Error : " + ex.getMessage());
        }
    }
}
